<template>
    <tnxvw-picker
        v-model="model"
        :title="title"
        :placeholder="placeholder"
        :empty="empty"
        :items="items"
        value-name="key"
        text-name="caption"
    />
</template>

<script>
import Picker from '../picker/Picker.vue';

export default {
    name: 'TnxvwEnumPicker',
    components: {
        'tnxvw-picker': Picker,
    },
    props: {
        model: {
            type: [String, Boolean],
            default: null,
        },
        title: String,
        placeholder: String,
        empty: [Boolean, String],
        type: {
            type: String,
            required: true,
        },
        subtype: String,
    },
    data() {
        return {
            items: [],
        }
    },
    watch: {
        model() {
            this.$emit('input', this.model);
        },
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            if (this.type) {
                if (this.type.toLowerCase() === 'boolean') {
                    this.items = [{
                        key: true,
                        caption: true.toText(),
                    }, {
                        key: false,
                        caption: false.toText(),
                    }];
                } else {
                    window.tnx.app.rpc.resolveEnumItems(this.type, this.subtype).then(items => {
                        this.items = items || [];
                        this.initModel();
                    });
                }
            }
        },
    }
}
</script>
